package com.gorkr.labuladong.data_struct.bt;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gorkr
 * @date 2022/06/17 12:08
 **/
public class E144BinaryTreePreorderTraversal {


    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private List<Integer> res = new ArrayList<>();

    public List<Integer> preorderTraversal(TreeNode root) {
        traverse(root);
        return res;

    }

    private void traverse(TreeNode root){
        if(root==null){
            return;
        }
        res.add(root.val);
        traverse(root.left);
        traverse(root.right);
    }
}
    